<?php

/**
 * This is the model class for table "{{express_log}}".
 *
 * The followings are the available columns in table '{{express_log}}':
 * @property integer $zel_id
 * @property integer $ze_id
 * @property integer $zel_type
 * @property integer $zel_start_num
 * @property double $zel_start_price
 * @property integer $zel_add_num
 * @property double $zel_add_price
 * @property string $zel_area
 * @property integer $zel_default
 */
class ExpressLog extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return ExpressLog the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{express_log}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ze_id, zel_type, zel_start_num, zel_start_price, zel_add_num, zel_add_price, zel_default', 'required'),
			array('ze_id, zel_type, zel_start_num, zel_add_num, zel_default', 'numerical', 'integerOnly'=>true),
			array('zel_start_price, zel_add_price', 'numerical'),
			array('zel_area', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zel_id, ze_id, zel_type, zel_start_num, zel_start_price, zel_add_num, zel_add_price, zel_area, zel_default', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zel_id' => 'Zel',
			'ze_id' => 'Ze',
			'zel_type' => 'Zel Type',
			'zel_start_num' => 'Zel Start Num',
			'zel_start_price' => 'Zel Start Price',
			'zel_add_num' => 'Zel Add Num',
			'zel_add_price' => 'Zel Add Price',
			'zel_area' => 'Zel Area',
			'zel_default' => 'Zel Default',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zel_id',$this->zel_id);
		$criteria->compare('ze_id',$this->ze_id);
		$criteria->compare('zel_type',$this->zel_type);
		$criteria->compare('zel_start_num',$this->zel_start_num);
		$criteria->compare('zel_start_price',$this->zel_start_price);
		$criteria->compare('zel_add_num',$this->zel_add_num);
		$criteria->compare('zel_add_price',$this->zel_add_price);
		$criteria->compare('zel_area',$this->zel_area,true);
		$criteria->compare('zel_default',$this->zel_default);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * getexpinfo
	 * 根据模板ID取信息
	 * Returns the total item count
	 */
	public function getexpinfo($ze_id){
		$criteria=new CDbCriteria;
		$criteria->compare('ze_id',$ze_id);
		$express = $this->find($criteria);
		return $express;
	}


	/**
	 * getexpprice
	 * 计算传过来的产品数据运费
	 * Returns the total item count
	 * @param 	$data array 产品数据
	 * @param 	$area int 配送区域	 
	 * @param 	$stype array 快递方式:1:快递,2:EMS,3:平邮, 与产品数据对应	
	 * @param 	$type int 计费方式:1:按件数，2:按重量，3:按体积 
	 * @return	array
	 */

	public function getexpprice( $data , $area = null , $stype ,$type = '1'){
			//print_r($data);exit;
			$expprice = 0;
			foreach ( (array)$data as $key => $value) {
				//查询模板计费方式
				$sql = new CDbCriteria(array(
								'select' => 'ze_valuation',
                                'condition' => 'ze_id = ' . $key ,                                
                            ));
				$express = Express::model()->find($sql);

				//取计费方式
				if( !empty($express->ze_valuation) ){
					$type = $express->ze_valuation;
				}
				if($type ==1){
					unset($value['weight']);
				}elseif($type ==2){
					unset($value['qty']);
				}else{
					unset($value['weight']);
				}
				//print_r($value);
				//取当前模板的相关信息
				$sql1 = new CDbCriteria(array(								
                                'condition' => 'ze_id = ' . $key ,                                
                            ));
				if($area != null){
					$sql1->condition .= ' AND FIND_IN_SET('.$area.', zel_area)';
				}else{
					$sql1->condition .= ' AND zel_default =2';
				}
				$sql1->condition .= ' AND zel_type = '.$stype[$key];
				
				$expinfo = $this->find($sql1);
				
				//如果跟地区查找不到快递模板,则使用默认模板
				if(empty($expinfo)){
					$sql2 = new CDbCriteria(array(								
                                'condition' => 'zel_default = \'2\'  AND ze_id = ' . $key ,                                
                            ));
                    $sql2->condition .= ' AND zel_type = '.$stype[$key];					
					$expinfo = $this->find($sql2);
				}
				//print_r($expinfo->zel_add_num);exit;
				//计算运费
				foreach ($value as $k => $v) {
					//print_r($v);
					//减去首重标准
					$k = $v - $expinfo->zel_start_num;
					//echo $expinfo->zel_add_price."*(".$k."/".$expinfo->zel_add_num.")";
					$expprice += $expinfo->zel_add_price*(ceil($k/$expinfo->zel_add_num));     //最大首费 + 增费金额 * (ceil(实际购买数量 / 增费标准)) 
				}
				//echo "<br>".$expinfo->zel_start_price."+".$expprice."<br>";
				$expprice = $expinfo->zel_start_price+$expprice ;
			}
			
			return $expprice;
		
	}


}